package com.zkr.kfgs.conterllor;


import com.zkr.kfgs.model.*;
import com.zkr.kfgs.service.KFGSService;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by lenovo on 2021/1/19.
 * 库房归属
 */
@RestController
@CrossOrigin
public class KFGSConterllor {

    @Autowired
    private KFGSService kfgsService;

    /**
     * 分页
     * @param
     * @return
     */
    @PostMapping("CompanyStorage/queryPage")
    public ResultVo queryPage(@RequestBody KFGSSelectVo kfgsSelectVo) {
        return kfgsService.queryPage(kfgsSelectVo);
    }

    /**
     * 根据库区编码找出库位信息
     */
    @PostMapping("queryKuWei")
    public ResultVo queryKuWei(String kqcode){
        return kfgsService.queryKuWei(kqcode);
    }

    /**
     * 修改启用禁用
     */
    @PostMapping("updateStatus")
        public ResultVo updateStatus(@RequestBody @Valid KFGSUpdateStatusVo kfgsUpdateStatusVo, BindingResult
        bindingResult){
            if (bindingResult.hasErrors()){
                return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
            }
            return kfgsService.updateStatus(kfgsUpdateStatusVo);
    }

    /*
    * 查询所有的法人
    * */

    @GetMapping("legalPerson/ofCurrentUserr")
    public ResultVo ofCurrentUser(){
        return kfgsService.ofCurrentUser();
    }

    //查找库房编码和库房名称
    @PostMapping("querywareHouse")
    public ResultVo queryWarehouse(@RequestBody KFGSVo kfgsVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kfgsService.queryWarehouse(kfgsVo);
    }

    //查看
    @GetMapping("CompanyStorage/queryCompanyStorageById")
    public ResultVo queryCompanyStorageById(Integer id){
        return kfgsService.queryCompanyStorageById(id);
    }


    //新增
    @PostMapping("storageBins/addUpdateBack")
    public ResultVo addUpdate(@RequestBody @Valid List<KFGSAddVo> addLegal, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kfgsService.addUpdate(addLegal);
    }


    /**
     * 根据库房编码查出库区信息
    @PostMapping("querykuqu")
    public ResultVo queryKuqu(String wareHouseCode){
        return kfgsService.queryKuqu(wareHouseCode);
    }*/


    //根据库房id查库区
    @PostMapping("querykuqu")
    public ResultVo queryKuqu(@RequestBody @Valid KQVo kqVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return kfgsService.queryWarehousess(kqVo);
    }


    /**
     * 库房属性下拉框
     */
    @GetMapping("querykqsx")
    public ResultVo querykqsx(){
        return kfgsService.querykqsx();
    }

    //查询法人已选的库房库区库位
    @GetMapping("FangQuWei")
    public ResultVo fangQuWei(Integer legalPersonId){
        if (legalPersonId == null){
            return ResultBuilderVo.error("法人id不存在");
        }
        return kfgsService.queryFangQuWei(legalPersonId);
    }

}








